﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCE.UI
{
    public partial class ZMCE90 : EFViewCmes
    {
        private EFTreeItem item;    //记录选中的树节点
        public ZMCE90()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ZMCEA0_Loaded);
        }
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZMCEA0_Loaded(object sender, RoutedEventArgs e)
        {
            this.tree.Refresh();

        }
        /// <summary>
        /// 选中树节点，导航右侧的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tree_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            item = this.tree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = this.GetTreeNodeInfo(item);
            if (item != null)
            {
                if (item.IsLeaf)
                {
                    this.container.LoadView("ZMCE92", args);
                }
                else
                {
                    this.container.LoadView("ZMCE91", args);
                }
            }
        }

        /// <summary>
        /// //获取tree选中的结点对应的label
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        private Dictionary<string, string> GetTreeNodeInfo(EFTreeItem item)
        {
            string fdParentId = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
            string fdParentName = item.ItemObject.GetType().GetProperty("text").GetValue(item.ItemObject, null) as string;
            string fdToencode = item.ItemObject.GetType().GetProperty("fdToencode").GetValue(item.ItemObject, null) as string;

            string isLeaf = (item.IsLeaf == true) ? "1" : "0";

            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("fdParentId", fdParentId);
            args.Add("isLeaf", isLeaf);
            args.Add("fdParentToencode", fdToencode);
            //args.Add("fdToencode", fdToencode);
            if (item.IsLeaf == false)
            {
                args.Add("fdParentName", fdParentName);
            }

            return args;
        }

        /// <summary>
        /// 展开树节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            if (treeItem.Level == 0)
            {
                this.tree.SelectedItem = treeItem;
                treeItem.ExpandItem();//展开
            }            
        }


        // /// <summary>
        // /// 重新基类的发布数据方法
        // /// 刷新树节点
        // /// </summary>
        // /// <param name="data"></param>
        public override void OnEIDataPublished(Platform.Core.EIInfo data)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string _value = "";
            if (item != null)
            {
                if (item.IsLeaf)
                {
                    _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("label").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                    args.Add("node", _value);
                }
                else
                {
                    _value = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
                    args.Add("node", _value);
                }
            }
            BaseTools.RefreshTree("ZMCE90", "query", args, OnDoWorkCompleted);
        }

        /// <summary>
        /// OnEIDataPublished回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.tree.EIDataContext = new Platform.Core.UI.EIDataContext();
            this.tree.EIDataContext.Update(e.EIInfo);
            item.IsLoadOnDemandEnabled = true;
            if (item.IsLeaf)
            {
                EFTreeItem parent = item.Parent as EFTreeItem;
                string node = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
                parent.Items.Clear();
                parent.CollapseItem();
                parent.ExpandItem();
                item = BaseTools.FindTreeItem(parent.Items, node, "label") as EFTreeItem;
                this.tree.SelectedItem = item;
                return;
            }
            item.Items.Clear();
            item.CollapseItem();
            item.ExpandItem();
        }

    }
}
